﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Thêm thư viện
using KaraokeManagementDTO;
using System.Data.OleDb;
using System.Data;

namespace KaraokeManagementDAO
{
    public class ChiTietPhieuTinhTienDAO:DataProvider
    {
        public Boolean themChiTiet(ChiTietPhieuTinhTienDTO dto)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van them chi tiet
            String query = "insert into ChiTietPhieuTinhTien(HangHoa_ID,SoLuong,DonGiaBan,GiamGia,PhieuTinhTien_ID) values(@HangHoa_ID,@SoLuong,@DonGiaBan,@GiamGia,@PhieuTinhTien_ID)";

            para.Add(new OleDbParameter("HangHoa_ID", dto.HangHoa_ID));
            para.Add(new OleDbParameter("SoLuong", dto.SoLuong));
            para.Add(new OleDbParameter("DonGiaBan", dto.DonGiaBan));
            para.Add(new OleDbParameter("GiamGia", dto.GiamGia));
            para.Add(new OleDbParameter("PhieuTinhTien_ID", dto.PhieuTinhTien_ID));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public Boolean xoaChiTiet(int MaChiTiet)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van xoa chi tiet
            String query = "delete from ChiTietPhieuTinhTien where ID=@MaChiTiet";

            para.Add(new OleDbParameter("@ID", MaChiTiet));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public Boolean capNhatChiTiet(ChiTietPhieuTinhTienDTO dto)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van cap nhat chi tiet
            String query = "update ChiTietPhieuTinhTien set HangHoa_ID=@HangHoa_ID,SoLuong=@SoLuong,DonGiaBan=@DonGiaBan,GiamGia=@GiamGia,PhieuTinhTien=@PhieuTinhTien where ID=@ID";

            para.Add(new OleDbParameter("HangHoa_ID", dto.HangHoa_ID));
            para.Add(new OleDbParameter("SoLuong", dto.SoLuong));
            para.Add(new OleDbParameter("DonGiaBan", dto.DonGiaBan));
            para.Add(new OleDbParameter("GiamGia", dto.GiamGia));
            para.Add(new OleDbParameter("PhieuTinhTien_ID", dto.PhieuTinhTien_ID));
            para.Add(new OleDbParameter("ID", dto.ID));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public ChiTietPhieuTinhTienDTO layThongTinChiTiet(int MaChiTiet)
        {
            ChiTietPhieuTinhTienDTO chitiet = new ChiTietPhieuTinhTienDTO();
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van lay thong tin chi tiet
            String query = "select * from ChiTietPhieuTinhTien where ID=@MaChiTiet";

            para.Add(new OleDbParameter("@ID", MaChiTiet));

            //DataTable
            DataTable dt = new DataTable();
            dt = ExecuteQuery(query, para);

            if (dt.Rows.Count == 0)
            {
                return null;
            }

            DataRow row = dt.Rows[0];

            chitiet.ID = int.Parse(row["ID"].ToString());
            chitiet.HangHoa_ID = int.Parse(row["HangHoa_ID"].ToString());
            chitiet.SoLuong = int.Parse(row["SoLuong"].ToString());
            chitiet.DonGiaBan = Double.Parse(row["DonGiaBan"].ToString());
            chitiet.GiamGia = Double.Parse(row["GiamGia"].ToString());
            chitiet.PhieuTinhTien_ID = int.Parse(row["PhieuTinhTien_ID"].ToString());

            return chitiet;
        }

        public List<ChiTietPhieuTinhTienDTO> layDanhSachChiTiet()
        {
            List<ChiTietPhieuTinhTienDTO> listChiTiet = new List<ChiTietPhieuTinhTienDTO>();

            List<OleDbParameter> para = new List<OleDbParameter>();

            //Câu truy vấn lấy dữ liệu
            String query = "select * from ChiTietPhieuTinhTien";


            //DataTable
            DataTable dt = new DataTable();
            dt = ExecuteQuery(query, para);

            if (dt.Rows.Count == 0)
            {
                return null;
            }

            foreach (DataRow row in dt.Rows)
            {
                ChiTietPhieuTinhTienDTO dto = new ChiTietPhieuTinhTienDTO();

                dto.ID = int.Parse(row["ID"].ToString());
                dto.HangHoa_ID = int.Parse(row["HangHoa_ID"].ToString());
                dto.SoLuong = int.Parse(row["SoLuong"].ToString());
                dto.DonGiaBan = Double.Parse(row["DonGiaBan"].ToString());
                dto.GiamGia = Double.Parse(row["GiamGia"].ToString());
                dto.PhieuTinhTien_ID = int.Parse(row["PhieuTinhTien_ID"].ToString());
                listChiTiet.Add(dto);
            }

            return listChiTiet;
        }
    }
}
